package development;

import iplayparticle.view.iplayparticleResources;
import state.DrawListener;
import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.SMSHandler;
import state.SendSMSListener;
import state.Sprite;
import state.TouchScreenListener;
import state.component.TextField;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.array.StringArray;

public class FrmResetPassword extends GameForm implements SendSMSListener, TouchScreenListener {
  private GameImage buttonImage;
  private ImageText text8;

  private int chooseIndex;

  private TextField userNameTextField;
  private Sprite sendButtonSprite;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmResetPassword();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    buttonImage = new GameImage(iplayparticleResources.image_button, null, null);

    userNameTextField = new TextField(_manager, TextField.CONSTRAINT_USER_NAME, Constants.TEXT_FIELD_STYLE);

    if (GameConstants.IS_240x320_SCREEN) {
      userNameTextField.setPosition(70, 155);
      userNameTextField.setSize(150, 20);
      sendButtonSprite = new Sprite(buttonImage, _manager, 85, 185, Constants.TOP_LEFT_ANCHOR);
    } else {
      userNameTextField.setPosition(128, 114);
      userNameTextField.setSize(150, 20);
      sendButtonSprite = new Sprite(buttonImage, _manager, 145, 144, Constants.TOP_LEFT_ANCHOR);
    }
    
    sendButtonSprite.setTouchScreenListener(this);
    sendButtonSprite.setDrawListener(new DrawListener() {
      public void paint(Sprite source, Graphics g) {
        if (chooseIndex == 1) {
          text8.drawString2(g, "Gửi", GameColor.YELLOW_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
        } else {
          text8.drawString2(g, "Gửi", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
        }
      }
    });

    chooseIndex = 0;// up first
    updateFocus();
    DrawUtils.setBottomBar(_manager, "Trở về", "", "Xóa");
  }

  private void drawFor240x320(Graphics g) {
    text8.drawString2(g, "Tài khoản", GameColor.WHITE_CODE, 12, 155, GameConstants.TOP_LEFT_ANCHOR);
    userNameTextField.draw(g);
  }

  private void drawFor320x240(Graphics g) {
    text8.drawString2(g, "Tài khoản", GameColor.WHITE_CODE, 52, 111, GameConstants.TOP_LEFT_ANCHOR);
    userNameTextField.draw(g);
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    if (GameConstants.IS_240x320_SCREEN) {
      drawFor240x320(g);
    } else {
      drawFor320x240(g);
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.ENTER:
      if (chooseIndex == 0) {
        userNameTextField.pointerReleased(userNameTextField, userNameTextField.getRealX(), userNameTextField.getRealY());
      } else if (chooseIndex == 1) {
        resetPassword();
      } 
      break;
    case Key.UP:
      chooseIndex--;
      if (chooseIndex < 0) {
        chooseIndex = 1;
      }
      updateFocus();
      break;
    case Key.DOWN:
      chooseIndex++;
      if (chooseIndex > 1) {
        chooseIndex = 0;
      }
      updateFocus();
      break;
    case Key.SOFT_LEFT:
      GameGlobal.nextState(FrmLogin.instance(), null);
      break;
    case Key.SOFT_RIGHT:
    case Key.BACKSPACE:
      if (chooseIndex == 0) {
        userNameTextField.deleteLastChar();
      }
      break;
    default:
      if (chooseIndex == 0) {
        userNameTextField.keyReleased1(keyCode);
      }
      break;
    }
  }

  private void resetPassword(){
    String username = userNameTextField.getText();
    if (username.length() < Constants.USER_NAME_MIN_LEN) {
      GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1("Tên đăng nhập phải", "dài hơn " + Constants.USER_NAME_MIN_LEN + " ký tự"));
      return;
    }

    if (username.length() > Constants.USER_NAME_MAX_LEN) {
      GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1("Tên đăng nhập phải", "ngắn hơn " + Constants.USER_NAME_MAX_LEN + " ký tự"));
      return;
    }
    
    // 7x85
    String[] sms = (String[]) Global.smsCommand.get("GAME_RESET_PASSWORD");
    // 9x79
    //String[] sms = (String[]) Global.smsCommand.get("GAME_RESET_PASSWORD_VIETTEL");
    StringBuffer smsCommand = new StringBuffer();
    smsCommand.append(sms[0]);
    smsCommand.append(" ");
    smsCommand.append(username);
    smsCommand.append(" ");
    smsCommand.append(Global.loginInfo.getUuid());
    smsCommand.append(" ");
    smsCommand.append(Global.provider.getId());
    if(Global.provider.getId() != Constants.TINHVAN_PROVIDER_ID){
      SMSHandler.getInstance(this).sendMsg(sms[1], smsCommand.toString());
    }else{
      SMSHandler.getInstance(this).sendMsg(sms[1], Constants.SMS_PREFIX +" "+ smsCommand.toString());
    }
  }

  public void updateFocus() {
    if (chooseIndex == 0) {
      userNameTextField.setFocused(true);
      DrawUtils.setBottomBar(_manager, "Trở về", "", "Xóa");
    } else if (chooseIndex == 1) {
      userNameTextField.setFocused(false);
      DrawUtils.setBottomBar(_manager, "Trở về", "Gửi", "");
    }
  }

  public void onSendSMSFail() {
    Global.alert.showAlert(this, GameAlert.OK_TYPE, "Gửi tin nhắn đăng ký thất bại");
  }

  public void onSendSMSSuccess() {
    Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create2("Gửi tin nhắn thành công.","Hệ thống sẽ gửi lại"," mật khẩu mới của bạn qua tin nhắn"));
  }
  
  public void destroy() {
    text8 = null;
    userNameTextField = null;
  }

  public void pointerDragged(Sprite source, int x, int y) {
  }

  public void pointerPressed(Sprite source, int x, int y) {
  }

  public void pointerReleased(Sprite source, int x, int y) {
    if (sendButtonSprite == source) {
      chooseIndex = 1;
      updateFocus();
      keyReleased(Key.FIRE);
    }
  }
}
